package com.wtgroup.demo.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 输入参数解析
 *
 * @author dafei
 * @version 0.1
 * @date 2021/4/28 16:12
 */
public class InputParseUtil {
    static char LEFT_SQ_BRACKET = '[';
    static char RIGHT_SQ_BRACKET = ']';
    static char COMMA = ',';
    static char SPACE = ' ';
    static char DB_QUOTE = '"';

    /**
     * 2021年4月28日
     * 目前仅支持 String 和 Integer 元素.
     * @date 2021年4月28日
     * @param arrLikeStr
     * @return
     */
    public static List parse(String arrLikeStr) {
        int n = arrLikeStr.length();
        int i = -1;
        Stack<Character> stack = new Stack<>();
        Stack<List> listStack = new Stack<>();
        String ele = null;
        List curList = null;
        boolean strType = false;
        while (++i<n) {
            char c = arrLikeStr.charAt(i);
            if ((c==SPACE || c==COMMA) && ele==null) {
                continue;
            }
            // space, ',', '"', ]
            if (c == SPACE || c == COMMA || c == RIGHT_SQ_BRACKET) {
                if (ele != null) {
                    if (!strType) {
                        curList.add(Integer.parseInt(ele));
                    } else {
                        curList.add(ele);
                    }
                    ele = null;
                    strType = false;
                }

                if (c == RIGHT_SQ_BRACKET) {
                    stack.pop();
                    curList = listStack.pop();
                    if (!listStack.isEmpty()) {
                        listStack.peek().add(curList);
                    }
                }
                continue;
            }

            if (c == LEFT_SQ_BRACKET) {
                stack.push(c);
                curList = new ArrayList();
                listStack.push(curList);
                continue;
            }
            if (c == DB_QUOTE) {
                if (ele == null) {
                    // 字符串开始
                    strType = true;
                }
                continue;
            }

            if(ele==null) ele = "";
            ele += c;
        }


        return curList;
    }

    /**
     * 二维list -> 二维array
     * @param input [[1,2,3],[]]
     * @return
     */
    public static int[][] listToArray2Int(List<List<Integer>> list) {
        int[][] res = new int[list.size()][];
        for (int i = 0, listSize = list.size(); i < listSize; i++) {
            Object ele = list.get(i);
            List one = (List) ele;
            int[] t = new int[one.size()];
            for (int i1 = 0; i1 < one.size(); i1++) {
                t[i1] = Integer.parseInt(one.get(i1).toString());
            }
            res[i] = t;
        }

        return res;
    }


    public static void main(String[] args) {
        String s = "[\"LRUCache\",\"get\",\"put\",\"get\",\"put\",\"put\",\"get\",\"get\"]";
        // String s = "[[2],[2],[2,6],[1],[1,5],[1,2],[1],[2]]";

        List res = parse(s);
        System.out.println(res);
    }

}
